/**
 * 
 */
package com.android.formula.database;

import java.io.IOException;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.android.formula.utils.Constants;
import com.android.formula.utils.LogUtils;

/**
 * @author HDH
 * 
 */
public class SQLiteTable {
	protected static final String TAG = "DataAdapter";

	Context mContext;
	SQLiteDatabase mDb;
	DatabaseHelper mDbHelper;

	public SQLiteTable(Context context) {
		this.mContext = context;
		mDbHelper = new DatabaseHelper(mContext, Constants.DB_FILE);
	}

	public SQLiteTable createDatabase() throws SQLException {
		try {
			mDbHelper.createDataBase();
		} catch (IOException mIOException) {
			LogUtils.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
			throw new Error("UnableToCreateDatabase");
		}
		return this;
	}

	public SQLiteTable open() throws SQLException {
		try {
			mDbHelper.openDataBase();
			mDbHelper.close();
			mDb = mDbHelper.getReadableDatabase();
		} catch (SQLException mSQLException) {
		    LogUtils.e(TAG, "open >>" + mSQLException.toString());
			throw mSQLException;
		}
		return this;
	}

	public void close() {
		mDbHelper.close();
	}
	
	public final SQLiteDatabase getDatabase(){
	    return mDb;
	}
}
